*************************************
*** Panel Analysis: NLSY97 *************
*************************************

clear
global OUT "SET_YOUR_OUTPATH"

// this is for data which is later read into R
global OUT2 "SET_YOUR_OUTPATH_FOR_DATAFRAMES"	


***create dummy datasets for regsave

*hybrid models
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_hhinc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_hhinc_dec_pp_sqrt.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_loginc.dta", replace

*bivariate 
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_hhinc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_hhinc_dec_pp_sqrt_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_loginc_0.dta", replace


*equivalence testing
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\NLSY97\nlsy97_coef_hhinc_dec_pp_sqrt_e.dta", replace






*load data
use "SET_YOUR_INPATH\NLSY97\nlsy97_long.dta", clear

xtset pid year

*remove outliers
replace hhinc=. if hhinc>400000 & hhinc<999999

*use hhinc with standardized hhinc
egen zhhinc = std(hhinc)
replace hhinc = zhhinc


// use income deciles from 1-10
rename hhinc_dec10 hhinc_dec10_
clonevar hhinc_dec10 = hhinc_dec 
 
rename hhinc_dec_pp10 hhinc_dec_pp10_
clonevar hhinc_dec_pp10 = hhinc_dec_pp 
 
rename hhinc_dec_pp_sqrt10 hhinc_dec_pp_sqrt10_
clonevar hhinc_dec_pp_sqrt10 = hhinc_dec_pp_sqrt 


// gen loginc
gen loginc = ln(hhinc_pp_sqrt)


*create interaction variables
gen hhincXage = hhinc * age18
gen hhinc_ppXage = hhinc_pp * age18
gen hhinc_pp_sqrtXage = hhinc_pp_sqrt * age18

gen hhinc_decXage = hhinc_dec10 * age18
gen hhinc_dec_ppXage = hhinc_dec_pp10 * age18
gen hhinc_dec_pp_sqrtXage = hhinc_dec_pp_sqrt10 * age18
*

* generate income change/jobloss variables

gen incdiff = hhinc_dec10-L.hhinc_dec10 

recode incdiff (-9/-1=1 "shock")(0/9=0 "no shock")(else=.), gen(incdiff1)
recode incdiff (-9/-2=1 "shock")(-1 0/9=0 "no shock")(else=.), gen(incdiff2)


*create engagement scales via factor analysis
reg vote polint10
gen mysample1 = e(sample)
polychoric vote polint10 if mysample1==1

global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) factors(1)
predict f1
sum f1
gen engagement = ((f1 - r(min)) / (r(max)-r(min)))*10


*labels
lab var vote "Vote"
lab var polint10 "Pol. Interest"
lab var engagement "Political Engagement Scale"


**************************************
***** Analysis ***********************
**************************************

estpost summarize vote polint10 ///
					hhinc hhinc_pp hhinc_pp_sqrt hhinc_dec10 hhinc_dec_pp10 hhinc_dec_pp_sqrt10 female age18 wkswk college citizen black race_other   ///
					hhinc_decXage hhinc_dec_ppXage hhinc_dec_pp_sqrtXage 
esttab using "$OUT\Tables\NLSY97\summary_stats_nlsy97.rtf", b(2) cells("count mean(fmt(3)) sd(fmt(3))  min max") replace

* regressions with age (continuous)

// for coef export
foreach var of varlist vote polint10 {
	xthybrid `var' hhinc, clusterid(pid) full
	eststo m001_`var'
	regsave W__hhinc B__hhinc using "$OUT2\NLSY97\nlsy97_coef_hhinc_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc female age18 wkswk college citizen black race_other, clusterid(pid) full
	eststo m01_`var'
	regsave W__hhinc B__hhinc using "$OUT2\NLSY97\nlsy97_coef_hhinc.dta", ci level(95) autoid append
	
	xthybrid `var' loginc, clusterid(pid) full
	eststo m002_`var'
	regsave W__loginc B__loginc using "$OUT2\NLSY97\nlsy97_coef_loginc_0.dta", ci level(95) autoid append
	xthybrid `var' loginc female age18 wkswk college citizen black race_other, clusterid(pid) full
	eststo m02_`var'
	regsave W__loginc B__loginc using "$OUT2\NLSY97\nlsy97_coef_loginc.dta", ci level(95) autoid append
	
	xthybrid `var' hhinc_dec_pp_sqrt10, clusterid(pid) full
	eststo m03_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\NLSY97\nlsy97_coef_hhinc_dec_pp_sqrt_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 wkswk college citizen black race_other, clusterid(pid) full
	eststo m3_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\NLSY97\nlsy97_coef_hhinc_dec_pp_sqrt.dta", ci level(95) autoid append
}

// for equivalence testing
foreach var of varlist vote polint10 {
	xtreg `var' hhinc_dec_pp_sqrt10 age18 wkswk, fe
	regsave hhinc_dec_pp_sqrt10 using "$OUT2\NLSY97\nlsy97_coef_hhinc_dec_pp_sqrt_e.dta", ci level(90) autoid append
}

// interactions
foreach var of varlist vote polint10 {
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 wkswk college citizen black race_other hhinc_dec_pp_sqrtXage, clusterid(pid) full
	eststo m7_`var'
}

*one table
esttab m3_vote m7_vote m3_polint10 m7_polint10  ///
	using "$OUT\Tables\NLSY97\hybrid_all_incdec_pp_sqrt.rtf", b(3) se replace	
esttab m3_vote m7_vote m3_polint10 m7_polint10  ///
	using "$OUT\Tables\NLSY97\hybrid_all_incdec_pp_sqrt.tex", ///
	b(3) se replace label nonumbers scalars("N N") booktabs alignment(D{.}{.}{3}) ///
	varlabels ///
				(R__female "Female" ///
				R__migrant "Migrant" ///
				R__citizen "US Citizen" ///
				R__college "Edu: college" ///
				R__black "Race: black" ///
				R__race_other "Race: other" ///
				W__wkswk "W: Job status" ///
				W__age18 "W: Age" ///
				B__wkswk "B: Job status" ///
				B__age18 "B: Age" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				_cons "Constant")	


				

// engagement scale:
xthybrid engagement hhinc_dec_pp_sqrt10 female age18 wkswk college citizen black race_other, clusterid(pid) full
eststo m_engage	
	
esttab m_engage ///
	using "$OUT\Tables\NLSY97\hybrid_engage_eqinc.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__female "Female" ///
				R__migrant "Migrant" ///
				R__citizen "US Citizen" ///
				R__college "Edu: college" ///
				R__black "Race: black" ///
				R__race_other "Race: other" ///
				W__wkswk "W: Job status" ///
				W__age18 "W: Age" ///
				B__wkswk "B: Job status" ///
				B__age18 "B: Age" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				_cons "Constant")				
				
				
	
* vote as logistic
xthybrid vote hhinc_dec_pp_sqrt10 female age18 wkswk college citizen black race_other, clusterid(pid) family(binomial) link(logit) full
eststo m5_vote_logit

esttab m5_vote_logit using "$OUT\Tables\NLSY97\nlsy97_hybrid_vote_logit.rtf", b(3) se replace
esttab m5_vote_logit using "$OUT\Tables\NLSY97\nlsy97_hybrid_vote_logit.tex", ///
	b(3) se replace label nonumbers scalars("N N") booktabs alignment(D{.}{.}{3}) ///
	varlabels ///
				(R__female "Female" ///
				R__migrant "Migrant" ///
				R__citizen "US Citizen" ///
				R__college "Edu: college" ///
				R__black "Race: black" ///
				R__race_other "Race: other" ///
				W__wkswk "W: Job status" ///
				W__age18 "W: Age" ///
				B__wkswk "B: Job status" ///
				B__age18 "B: Age" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				_cons "Constant")















